package CH5;

/**
 * Created by Cai Ye on 2016/3/30.
 */

import java.util.ArrayDeque;
import java.util.Deque;

import static java.lang.System.out;

public class CollectionStack {
    private Deque elems = new ArrayDeque();
    private int capacity;

    public CollectionStack(int capacity) {
        this.capacity = capacity;
    }

    public static void main(String[] args) {
        CollectionStack stack = new CollectionStack(5);
        stack.push("Justin");
        stack.push("Monica");
        stack.push("Irene");
        out.println(stack.pop());
        out.println(stack.pop());
        out.println(stack.pop());
    }

    public boolean push(Object elem) {
        if (isFull()) {
            return false;
        }
        return elems.offerLast(elem);
    }

    private boolean isFull() {
        return elems.size() + 1 > capacity;
    }

    public Object pop() {
        return elems.pollLast();
    }

    public Object peek() {
        return elems.peekLast();
    }

    public int size() {
        return elems.size();
    }
}
